package com.samoiloff.ui.btns.frame {
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	/**
	 * @author NM Group
	 */
	public class FrameBtnController extends EventDispatcher {
		
		protected var _mc: MovieClip;
		
		public function FrameBtnController(mc:MovieClip) {
			_mc = mc;
			_mc.useHandCursor = _mc.buttonMode = true;
			
			_mc.mouseChildren = false;
			_mc.addEventListener(MouseEvent.CLICK, onMouseClick);
			_mc.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			_mc.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_mc.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			
			onMouseOut();
		}
		
		public function destroy():void {
			_mc.removeEventListener(MouseEvent.CLICK, onMouseClick);
			_mc.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			_mc.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_mc.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_mc = null;
		}
		
		protected function onMouseClick(e:MouseEvent): void {
			if ((_mc.currentFrame != 4) && (_mc.currentFrame != 5)) {
				_mc.gotoAndStop(2);
				dispatchEvent(e);
			}
		}

		protected function onMouseOver(e:MouseEvent): void {
			if ((_mc.currentFrame != 4) && (_mc.currentFrame != 5)) {
				_mc.gotoAndStop(2);
			}
			dispatchEvent(e);
		}

		protected function onMouseOut(e : MouseEvent = null) : void {
			if ((_mc.currentFrame != 4) && (_mc.currentFrame != 5)) {
				_mc.gotoAndStop(1);
			}
			if (e) {
				dispatchEvent(e);
			}
		}

		protected function onMouseDown(e:MouseEvent): void {
			if ((_mc.currentFrame != 4) && (_mc.currentFrame != 5)) {
				_mc.gotoAndStop(3);
				dispatchEvent(e);
			}
		}

		public function get mc(): MovieClip {
			return _mc;
		}
		
	}
}
